﻿using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using System.Collections;
using NeonMika.NETMF;
using NeonMika.Webserver;
using NeonMika.Webserver.EventArgs;
using NeonMika.Webserver.Responses;
using CodingSmackdown.Services;

namespace CodingSmackdown.TemperatureMonitor
{
    public class Program
    {
        private static Thread mainThread;

        public static void Main()
        {
            mainThread = Thread.CurrentThread;

            Settings settings = new Settings();
            settings.loadSettings();

            UpdateSettingsMethod.UpdateNetworkConfiguration(settings);

            Server WebServer = new Server(80);
            WebServer.AddResponse(new JSONResponse("temperature", new JSONResponseCheck(GetTemperatureMethod.GetTemperature)));
            WebServer.AddResponse(new JSONResponse("settings", new JSONResponseCheck(GetSettingsMethod.GetSettings)));
            WebServer.AddResponse(new JSONResponse("history", new JSONResponseCheck(GetHistoryMethod.GetHistory)));
            WebServer.AddResponse(new JSONResponse("updateSettings", new JSONResponseCheck(UpdateSettingsMethod.UpdateSettings)));

            NTPTimeService timeService = new NTPTimeService();
            timeService.SystemSettings = settings;
            timeService.Start();

            Thread.Sleep(5000);

            NameService nameService = new NameService();
            nameService.AddName(settings.NetBiosName, NameService.NameType.Unique, NameService.MsSuffix.Default);

            Thread.Sleep(5000);

            TemperatureLoggingService tempLogger = new TemperatureLoggingService();
            tempLogger.SystemSettings = settings;
            tempLogger.Start();

            mainThread.Suspend();

        }
    }
}
